package com.myserver.core;

import com.myserver.log.LoggerFactory;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Logger;

public class Server{
    private ServerSocket server;
    final Logger logger = LoggerFactory.getLogger();

    /**
     * 启动服务器
     */
    public void start() {
        try {
            server = new ServerSocket(8888);
            logger.info("服务器启动");
            receive();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 接收客户端请求
     */
    private void receive() {

        while (true) {
            try {
                Socket socket = server.accept();
                logger.info("收到客户端请求");
                new Dispatcher(socket).start();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public void stop() {
        try {
            server.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Server server = new Server();
        server.start();
    }
}
